package com.ham.mud.state;

import com.ham.mud.Colors;
import com.ham.mud.ServerConnection;
import com.ham.mud.characters.player.Player;
import com.ham.mud.commands.PlayerCommand;
import com.ham.mud.commands.skills.FleeSkill;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hlucas on Jun 30, 2011 at 3:23:39 PM
 */
public class PVPState extends ConnectionState {
    ServerConnection enemy;
    Player enemyPlayer;
    Player player;
    List<String> inputBuffer = new ArrayList<String>();
    boolean inCombat = true;
    boolean isVictor = false;
    boolean isFirstTick = true;

    public PVPState(ServerConnection connection, ServerConnection enemy) {
        super(connection);
        this.enemy = enemy;
        this.enemyPlayer = enemy.getPlayer();
        this.player = connection.getPlayer();
        connection.println("You are now engaged in PVP combat!");
    }

    public ServerConnection getEnemy() {
        return enemy;
    }

    @Override
    public void processInput(String line) {
        if(line == null || line.trim().equals("")) {
            connection.println();
            connection.showPrompt();
            return;
        }

        inputBuffer.add(line);
    }

    @Override
    public void preTick() {
        if(isFirstTick) {
            isFirstTick = false;
            connection.println();
            return;
        }

        connection.println(Colors.NEW_LINE);
    }

    @Override
    public void tick() {

        if(player.getVitals().getHealth() <= 0) {
            connection.println("You have been slain!" + Colors.NEW_LINE);
            inCombat = false;
            return;
        }

        boolean didFlee = processInputBuffer();
        if(didFlee) {
            inCombat = false;
            return;
        }

        int playerDamageRoll = player.getCombatDamageRoll();

        if(playerDamageRoll > 0) {
            connection.println("You hit " + enemyPlayer.getName() + " for " + playerDamageRoll + " damage.");
        } else {
            connection.println("You swing at " + enemyPlayer.getName() + " and miss.");
        }

        if(playerDamageRoll > 0) {
            enemy.println(player.getName() + " hits you for " + playerDamageRoll + " damage.");
        } else {
            enemy.println(player.getName() + " swings at you and misses.");
        }

        enemyPlayer.getVitals().decrementHealth(playerDamageRoll);
        if(enemyPlayer.getVitals().getHealth() <= 0) {
            inCombat = false;
            connection.println("You have slain " + enemyPlayer.getName() + "!" + Colors.NEW_LINE);
            isVictor = true;
        }
    }

    private boolean processInputBuffer() {
        PlayerCommand command = CombatState.processInputBuffer(connection, inputBuffer);
        if(command != null) {
            command.execute(connection);
            if(command instanceof FleeSkill) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void postTick() {
        if(!inCombat) {
            connection.showPrompt();
            connection.changeState(new PlayingState(connection));
            if(isVictor) {
                connection.printSolo("You disengage from PVP combat as the victor!");
            } else {
                connection.printSolo("You disengage from PVP combat as the loser!");
            }
        } else {
            connection.println(CombatState.getCombatantHealthString(enemyPlayer));
            connection.showPrompt();
        }
    }
}
